﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.BLL;
using System.Data;
using Yipusoft.Exercitation.DBUtility;

public partial class Combat_SquadronStat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();            
            Bind();
        }
    }
    private void InitData()
    {
        for (int i = 2010; i <= 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        ddlYear.SelectedValue = string.IsNullOrEmpty(Request["Year"]) ? DateTime.Now.Year.ToString() : Request["Year"];
        ddlMonth.SelectedValue = string.IsNullOrEmpty(Request["Month"]) ? DateTime.Now.Month.ToString() : Request["Month"];

        ddlStation.DataSource = CodeItem.GetList(" ParentID='SBGW0000'");
        ddlStation.DataTextField = "itemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();
        ddlStation.Items.Insert(0, new ListItem("全部", "0000"));

        ddlStation.SelectedValue = string.IsNullOrEmpty(Request["StationID"]) ? "0000" : Request["StationID"];
        
        ddlRank.DataSource = CodeItem.GetList(" ParentID='ZXSBJX00'");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();
        ddlRank.Items.Insert(0, new ListItem("全部", "0000"));

        ddlRank.SelectedValue = string.IsNullOrEmpty(Request["RankID"]) ? "0000" : Request["RankID"];

        ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" Right(DepartmentID,3)='000' And DepartmentID<>'1300000'");
        ddlDepartment.DataTextField = "ShortName";
        ddlDepartment.DataValueField = "DepartmentID";
        ddlDepartment.DataBind();
        string departmentID = string.IsNullOrEmpty(Request["DepartmentID"]) ? Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID : Request["DepartmentID"];
        ddlDepartment.SelectedValue = departmentID;
    }
    private void Bind()
    {
        string departmentID = ddlDepartment.SelectedValue;
        List<Yipusoft.Account.Model.DepartmentInfo> departmentList = Yipusoft.Account.BLL.Department.GetList(" departmentID<>'"+departmentID +"'  And DepartmentID IN (Select DepartmentID From TaskSquadron Where TaskID='" + GetTaskID() + "' And Left(DepartmentID,4)='" + departmentID.Substring(0,4) + "') Order by DepartmentID");
        DataTable dtb = new DataTable();
        DataColumn clm;
        DataRow drw;
        clm = new DataColumn("DepartmentID", typeof(System.String));
        dtb.Columns.Add(clm);
        clm = new DataColumn("DepartmentName", typeof(System.String));
        dtb.Columns.Add(clm);
        clm = new DataColumn("InputScore", typeof(System.Decimal)); //录入率
        dtb.Columns.Add(clm);
        clm = new DataColumn("Ach", typeof(System.Decimal));//达标率
        dtb.Columns.Add(clm);
        clm = new DataColumn("SumAch", typeof(System.Decimal));//总达标率
        dtb.Columns.Add(clm);

        foreach (Yipusoft.Account.Model.DepartmentInfo dpt in departmentList)
        {
            drw = dtb.NewRow();
            drw["DepartmentID"] = dpt.DepartmentID;
            drw["DepartmentName"] = dpt.ShortName;
            drw["InputScore"] = GetInputScore(dpt.DepartmentID);
            drw["Ach"] = GetAchScore(dpt.DepartmentID);
            drw["SumAch"] = GetSumScore(dpt.DepartmentID);
            dtb.Rows.Add(drw);

        }
        UserChart.Series["录入率"].Points.DataBindXY(dtb.DefaultView, "DepartmentName", dtb.DefaultView, "InputScore");
        UserChart.Series["达标率"].Points.DataBindXY(dtb.DefaultView, "DepartmentName", dtb.DefaultView, "Ach");
        UserChart.Series["总达标率"].Points.DataBindXY(dtb.DefaultView, "DepartmentName", dtb.DefaultView, "SumAch");

        for (int i = 0; i < dtb.Rows.Count; i++)
        {
            UserChart.Series["录入率"].Points[i].Href = "InputList.aspx?DepartmentID=" + dtb.Rows[i][0].ToString() + "&Year=" + ddlYear.SelectedValue + "&Month=" + ddlMonth.SelectedValue + "&StationID=" + ddlStation.SelectedValue + "&RankID=" + ddlRank.SelectedValue;
            UserChart.Series["达标率"].Points[i].Href = "InputList2.aspx?DepartmentID=" + dtb.Rows[i][0].ToString() + "&Year=" + ddlYear.SelectedValue + "&Month=" + ddlMonth.SelectedValue + "&StationID=" + ddlStation.SelectedValue + "&RankID=" + ddlRank.SelectedValue; ;
            UserChart.Series["总达标率"].Points[i].Href = "InputList3.aspx?DepartmentID=" + dtb.Rows[i][0].ToString() + "&Year=" + ddlYear.SelectedValue + "&Month=" + ddlMonth.SelectedValue + "&StationID=" + ddlStation.SelectedValue + "&RankID=" + ddlRank.SelectedValue; ;
        }
    }

    public string GetTaskID()
    {
        TaskBaseInfo taskInfo = TaskBase.GetList(" Year(EndDate)=" + ddlYear.SelectedValue + " And Month(EndDate)=" + ddlMonth.SelectedValue + "").FirstOrDefault();
        return taskInfo != null ? taskInfo.TaskID.ToString() : "";
    }
    public string GetRankAndStation()
    {
        string where = string.Empty;
        if (!ddlRank.SelectedValue.Equals("0000"))
        {
            where = " And RankID='" + ddlRank.SelectedValue + "'";
        }
        if (!ddlStation.SelectedValue.Equals("0000"))
        {
            where += " And StationID='" + ddlStation.SelectedValue + "'";
        }

        return where;
    }

    //录入率
    public decimal GetInputScore(string departmentID)
    {
        if (departmentID == "1301301")
        {
            string aa = "";
        }
      
        string where = GetRankAndStation();       
        //应录入人数
        string sql = "Select Count(*) From (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "' Group by SoldierID) ##t";
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());
        
        //已录入人数
        sql = "Select Count(*) From (Select SoldierID From TaskScore Where  SoldierID IN (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "') And TaskID='" + GetTaskID() + "' And DepartmentID='" + departmentID + "'  Group by SoldierID) ##t1";
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        if (count > 0 )
            return (count2 / count) * 100;
        else
            return 0;
    }
    //达标率 
    public decimal GetAchScore(string departmentID)
    {
        string where = GetRankAndStation();
       
        //达标人数
        string sql = "Select Count(*) From	(Select SoldierID From TaskSoldierScore Where Score=1 And SoldierID IN (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "')  And TaskID='" + GetTaskID() + "' And DepartmentID='" + departmentID + "'   Group by SoldierID) ##t3";
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        //已录入人数
        sql = "Select Count(*) From	(Select SoldierID From TaskScore Where  SoldierID IN (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "')  And TaskID='" + GetTaskID() + "' And DepartmentID='" + departmentID + "'   Group by SoldierID) ##t4";
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        if (count2>0)
            return (count / count2) * 100;
        else
            return 0;
    }

    public decimal GetSumScore(string departmentID)
    {
        string where = GetRankAndStation();
        //达标人数
        //string sql = "Select Count(*) From	(Select SoldierID From TaskSoldierScore Where Score=1 And SoldierID IN (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "')  And TaskID='" + GetTaskID() + "' And DepartmentID='" + departmentID + "'   Group by SoldierID) ##t5";
        string sql = " Select Count(*) From TaskSoldierScore Where DepartmentID='" + departmentID + "' And TaskID='" + GetTaskID() + "' And Score=1 And SoldierID IN (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "')";
        decimal count = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        //应录入人数
        sql = "Select Count(*) From (Select SoldierID From TaskSoldiers Where DepartmentID='" + departmentID + "'" + where + " And TaskID='" + GetTaskID() + "' Group by SoldierID)   ##t6";
        decimal count2 = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.CONN_STRING, CommandType.Text, sql).ToString());

        if (count2 > 0)
            return (count / count2) * 100;
        else
            return 0;
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
       
        Bind();
    }
}
